import { Agent } from "http";

{
    let obj = {
        time: '2019-01-11',
        name: 'net',
        _r: 123
    }

    let monitor = new Proxy(obj, {
        //拦截对象属性的读取
        get(target, key) {
            return target[key].replace('2017', '2018')
        },
        set(target, key, value) {
            if (key === 'name') {
                return target[key] = value;
            } else {
                return target[key]
            }
        },
        //拦截key in object操作
        has(target, key) {
            if (key === 'name') {
                return target[key]
            } else {
                return false
            }
        },
        //拦截delete
        deleteProperty(target, key) {
            if (key.indexOf('_') > -1) {
                delete target[key]
                return true
            } else {
                return target[key]
            }
        },
        //拦截Object.keys,Object.getOwnPropertySymbols,Object.getOwnPropertyName
        ownKeys(target) {
            return Object.keys(target).filter(item => item != 'time')
        }
    })
    console.log('get', monitor.time)

    monitor.time = '2018'
    monitor.name = 'huangliusong'
    console.log('set', monitor.time, monitor)

    console.log('has', 'name' in monitor, 'time' in monitor);


    console.log('ownKeys', Object.keys(monitor));

}


{
    let obj = {
        time: '2017-03-11',
        name: 'net',
        _r: 123
    }

    console.log("Reflect", Reflect.get(obj, 'time'));
    Reflect.set(obj, 'name', 'huangliusong')
    console.log(obj);
    console.log('has', Reflect.has(obj, 'name'))

}
{
    console.log('\n\n\n\n')
}



{
    let obj = {
        time: '2019-01-01',
        name: 'net',
        _r: 123
    }

    let monitor = new Proxy(obj, {
        //拦截
        get(target, key) {
            console.log('get start')
            return target[key].replace('2019', '2020')
        },
        //拦截设置属性
        set(target, key, value) {
            if (key === 'name') {
                console.log('修改了');

                return target[key] = value
            } else {
                console.log('不能修改')
                return '不能修改'
            }
        },
        //拦截key in object 操作
        has(target, key, value) {
            if (key === 'name') {
                return target[key]
            } else {
                return false
            }
        },

        //拦截delete
        deleteProperty(target, key) {
            if (key.indexOf('_') > -1) {
                //如果是包含_ 则可以删除
                delete target[key]
                return true
            } else {
                return target[key]
            }
        },
        //
        ownKeys(target) {
            return Object.keys(target).filter(item => {
                return item != 'time'
            })
        }
    })

    console.log('get:', monitor.time);
    monitor.time = '2018'
    monitor.name = '小小'
    console.log(monitor);
    console.log('has', 'time' in monitor);
    // delete monitor.time
    // console.log(monitor);
    // delete monitor._r
    // console.log(monitor);

    console.log('ownKeys', Object.keys(monitor));

}

{
    let obj = {
        time: '2019',
        name: 'hls',
        _r: 342
    }
    console.log('reflect get', Reflect.get(obj, 'time'));
    Reflect.set(obj, 'name', 'huangliusong')
    console.log(obj);
    console.log('has', Reflect.has(obj, 'naem'));
}


{
    function valedator(target, validator) {
        return new Proxy(target, {
            _validator: valedator,
            set(target, key, value, proxy) {
                if (target.hasOwnProperty(key)) {
                    let va = this._validator[key]
                    if (!!va(value)) {
                        return Reflect.set(target, key, value, proxy)
                    } else {
                        throw Error(`不能设置${key}到${value}`)
                    }
                } else {
                    throw Error(`${key}不存在`)
                }
            }
        })
    }
    const personValidators = {
        name(val) {
            return typeof val === 'string'
        }
    }
}



